package com.yiwei.zxp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.yiwei.zxp.R;
import com.yiwei.zxp.activity.BussinessMyCommentActivity;
import com.yiwei.zxp.activity.LogisticsActivity;
import com.yiwei.zxp.adapter.BussinessOrderAdapter;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.BuyerOrderDetailBean;
import com.yiwei.zxp.api.bean.BuyerOrderListBean;
import com.yiwei.zxp.api.bean.ExpressSendBean;
import com.yiwei.zxp.event.BussinessOrderNeedRefreshEvent;
import com.yiwei.zxp.event.LoginSuccessEvent;
import com.yiwei.zxp.event.SelectQuickInfoEvent;
import com.yiwei.zxp.util.Dialog;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.Util;
import com.yiwei.zxp.view.OnLoadeMoreListener;
import com.yiwei.zxp.view.QuickInfoDilaog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-6-15.
 */

public class BussinessOrderFragment extends BaseFragment implements BussinessOrderAdapter.OnChooseListener {
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    Unbinder unbinder;
    private boolean needRefresh = false;
    private int id;
    private OnLoadeMoreListener listener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            getBussinessOrder(page);
        }
    };
    private BussinessOrderAdapter adapter;
    private QuickInfoDilaog quickInfoDialog;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_bussiness_order;
    }

    @Override
    protected void initView() {
        if (getArguments() != null) {
            id = getArguments().getInt("id", 0);
        }
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                listener.setPage(1);
                getBussinessOrder(1);
            }
        });
        adapter = new BussinessOrderAdapter(getContext());
        adapter.setOnChooseListener(this);
        list.addOnScrollListener(listener);
        list.setAdapter(adapter);
        quickInfoDialog = new QuickInfoDilaog(getContext()).setOnChooseListener(new QuickInfoDilaog.OnChooseListener() {
            @Override
            public void onSuccess(String order_no, String express_id, String express_no) {
                getExpressSend(order_no, express_no, express_id);
            }
        });
        EventBus.getDefault().register(this);
        listener.setPage(1);
        getBussinessOrder(1);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (needRefresh) {
            listener.setPage(1);
            getBussinessOrder(1);
            needRefresh = false;
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(BussinessOrderNeedRefreshEvent event) {
        needRefresh = true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(SelectQuickInfoEvent event) {
        quickInfoDialog.setTxtQuickName(event.id, event.name);
    }

    public void getBussinessOrder(int page) {
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.show(getContext(), "网络连接失败");
            return;
        }

        refresh.setRefreshing(true);
        BuyerOrderListBean bean = new BuyerOrderListBean();
        bean.page = page;
        bean.status = id;
        Api.api_service.getSellOrderList(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.add(result.datas, page == 1);
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                    }
                });
    }


    public void getExpressSend(String order_no, String express_no, String express_id) {
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.show(getContext(), "网络连接失败");
            return;
        }

        refresh.setRefreshing(true);
        ExpressSendBean bean = new ExpressSendBean();
        bean.express_id = express_id;
        bean.express_no = express_no;
        bean.order_no = order_no;
        Api.api_service.getExpressSend(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(getContext(), "发货成功");
                    listener.setPage(1);
                    getBussinessOrder(1);
                });
    }


    public void getOrderDelete(String order_no) {
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.show(getContext(), "网络连接失败");
            return;
        }

        refresh.setRefreshing(true);
        BuyerOrderDetailBean bean = new BuyerOrderDetailBean();
        bean.order_no = order_no;
        Api.api_service.getBuyerOrderDelete(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(getContext(), "删除成功");
                    listener.setPage(1);
                    getBussinessOrder(1);
                });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (quickInfoDialog != null) {
            quickInfoDialog.dismiss();
            quickInfoDialog = null;
        }
        unbinder.unbind();
        EventBus.getDefault().unregister(this);

    }

    @Override
    public void onPay(String order_no) {
        ToastUtil.makeText(getContext(), "提醒成功");
    }

    @Override
    public void onSendTips(String order_no) {
        quickInfoDialog.setOrder_no(order_no);
        quickInfoDialog.show();
    }

    @Override
    public void onSure(String order_no) {
        ToastUtil.makeText(getContext(), "提醒成功");
    }

    @Override
    public void onWuliu(String order_no) {
        Intent intent = new Intent(getActivity(), LogisticsActivity.class);
        intent.putExtra("order_no", order_no);
        startActivity(intent);
    }

    @Override
    public void onDelete(String order_no) {
        Dialog.showDefaultDialog(getContext(), "", "确定要删除该订单吗？", "取消", "确定", false, new Dialog.DialogDefaultClickListener() {
            @Override
            public void confirm() {
                getOrderDelete(order_no);
            }

            @Override
            public void cancel() {

            }
        });
    }

    @Override
    public void onComment() {
        Util.startActivity(getContext(), BussinessMyCommentActivity.class);
    }
}
